var index = 0;
var flag = true;
var timer;

function runSlide() {
    var picture = $("#wrap #pic-slide #pic-slide-show");
    var len = picture.find("li").length;
    var list_dot = $("#wrap #pic-slide #list-dot li");

    flag = false;
    if (index == len - 2) {
        list_dot.eq(index).removeClass('current-dot');
        list_dot.eq(0).addClass('current-dot');
    } else {
        list_dot.eq(index).removeClass('current-dot');
        list_dot.eq(index + 1).addClass('current-dot');
    }
    if (index < len - 1) {
        picture.animate({
            left: '-=480px'
        }, 700, function() {
            if (index == len - 1) {
                picture.css('left', '0px');
                index = 0;
            }
            flag = true;
        });
        index++;
    }
}

$(document).ready(function() {
    $(window).scroll(function() {
        var top = $(document).scrollTop();
        var navigate = $('#navigate');
        var items = $('.album-item');
        var currentId = "";

        items.each(function() {
            var m = $(this);
            var itemTop = m.offset().top;
            if (top > itemTop - 150) {
                currentId = '#' + m.attr('id');
            } else {
                return false;
            }
        });

        var currentLink = navigate.find('#navigate-link').find('.current');
        if (currentId && currentLink.attr('href') != currentId) {
            currentLink.removeClass('current');
            navigate.find("[href = " + currentId + "]").addClass('current');
        }
    });

    if (flag) {
        timer = setInterval(runSlide, 4000);
    }

    $("#wrap #pic-slide").mouseover(function() {
        $("#wrap #pic-slide .switch-button").show();
    });

    $("#wrap #pic-slide").mouseout(function() {
        $("#wrap #pic-slide .switch-button").hide();
    });

    $("#wrap #pic-slide #switch-left").click(function() {
        var picture = $("#wrap #pic-slide #pic-slide-show");
        var len = picture.find("li").length;
        var list_dot = $("#wrap #pic-slide #list-dot li");

        if (!flag) {
            return;
        }
        flag = false;
        clearInterval(timer);
        if (index == len - 2) {
            list_dot.eq(index).removeClass('current-dot');
            list_dot.eq(0).addClass('current-dot');
        } else {
            list_dot.eq(index).removeClass('current-dot');
            list_dot.eq(index + 1).addClass('current-dot');
        }
        if (index < len - 1) {
            picture.animate({
                left: "-=480px"
            }, 700, function() {
                if (index == len - 1) {
                    index = 0;
                    picture.css('left', '0px');
                }
                flag = true;
                timer = setInterval(runSlide,4000);
            });
            index++;
        }
    });

    $("#wrap #pic-slide #switch-right").click(function() {
        var picture = $("#wrap #pic-slide #pic-slide-show");
        var len = picture.find("li").length;
        var list_dot = $("#wrap #pic-slide #list-dot li");

        if (!flag) {
            return;
        }
        flag = false;
        clearInterval(timer);
        if (index == 0) {
            list_dot.eq(index).removeClass('current-dot');
            list_dot.eq(len - 2).addClass('current-dot');
        } else {
            list_dot.eq(index).removeClass('current-dot');
            list_dot.eq(index - 1).addClass('current-dot');
        }
        if (index == 0) {
            index = len - 1;
            picture.css('left', index * -480);
        };
        index--;
        picture.animate({
            left: "+=480px"
        }, 700, function() {
            flag = true;
            timer = setInterval(runSlide,4000);
        });
    });
});
